/*
 * Copyright (c) 2008, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _TANSU_SSDP_H_
#define _TANSU_SSDP_H_

#include "tansu_status.h"
#include "tansu_util.h"
#include "tansu_nwif.h"
#include "tansu_socket.h"

#define TANSU_SSDP_IPv4_ADDR            "239.255.255.250"
#define TANSU_SSDP_IPv4_PORT            1900
                                        
#define TANSU_SSDP_NTS_ALIVE            "ssdp:alive"
#define TANSU_SSDP_NTS_BYEBYE           "ssdp:byebye"

#define TANSU_SSDP_MX_DEFAULT           2

#define TANSU_SSDP_ARENA_BUFFER_SIZE    1024
#define TANSU_SSDP_ARENA_CHUNK_SIZE     32

enum tansu_ssdp_search_target {
    TANSU_SSDP_ST_INVALID,
    TANSU_SSDP_ST_SSDP_ALL,
    TANSU_SSDP_ST_UPNP_ROOTDEVICE,
    TANSU_SSDP_ST_UUID,
    TANSU_SSDP_ST_UPNP_DEVICE,
    TANSU_SSDP_ST_UPNP_SERVICE,
    TANSU_SSDP_ST_VENDOR_DEVICE,
    TANSU_SSDP_ST_VENDOR_SERVICE,
};

enum tansu_ssdp_nts {
    TANSU_NTS_UNKNOWN,
    TANSU_NTS_ALIVE,
    TANSU_NTS_BYEBYE,
};

enum tansu_ssdp_kind {
    TANSU_SSDP_REQUEST,
    TANSU_SSDP_RESPONSE,
};

struct tansu_ssdp_packet {
    enum tansu_ssdp_kind kind;
    struct sockaddr_storage remote;
    union {
        struct {
            char *method;
            char *uri;
            char *version;
        } req;
        struct {
            char *version;
            int  status;
            char *readable;
        } res;
    };
    struct tansu_keyvals headers;
};

struct tansu_ssdp_ad {
    STAILQ_ENTRY(tansu_ssdp_ad) next;
    const char *udn;
    const char *nts;
    const char **nt;
    int nt_size;
    int current;
    int max_age;
    uint16_t port;
    struct tansu_keyvals *headers;
};

struct tansu_ssdp_request {
    STAILQ_ENTRY(tansu_ssdp_request) next;
    struct tansu_ssdp_arena *arena;
	struct tansu_bound_sockets sockets;
    char *st;
    int mx;
    int count;
    struct timeval tv;
    const struct tansu_ssdp_addr *mcast_addr;
    void (*cb)(struct tansu_ssdp_packet *, void *);
    void *cbarg;
    void (*finish_cb)(struct tansu_ssdp_request *, void *);
    void *finish_cbarg;
    struct tansu_keyvals *headers;
};

struct tansu_ssdp_listener {
    STAILQ_ENTRY(tansu_ssdp_listener) next;
    void (*cb)(const struct tansu_ssdp_packet *, void *);
    void *cbarg;
};

struct tansu_ssdp_arena {
    struct tansu_ssdp_packet packet;
    char buff[TANSU_SSDP_ARENA_BUFFER_SIZE];
    struct tansu_keyval chunk[TANSU_SSDP_ARENA_CHUNK_SIZE];
};

struct tansu_ssdp_response {
    STAILQ_ENTRY(tansu_ssdp_response) next;
    const char *udn;
    const char **st;
    int st_size;
    int current;
    int max_age;
    uint16_t port;
    struct sockaddr_storage remote;
    struct tansu_keyvals *headers;
};

struct tansu_ssdp_addr {
    const char *addr;
    struct sockaddr_storage ss;
};

struct tansu_ssdp_interface {
    STAILQ_ENTRY(tansu_ssdp_interface) next;
    int fd;
    char *name;
    char *mcastif;
    const struct tansu_ssdp_addr *mcast_addr;
};

struct tansu_ssdp_server {
    struct event_base *evbase;
    int recv_fd;
    struct event recv_ev;
    struct event send_ev;
    struct event ad_ev;
    STAILQ_HEAD(tansu_ssdp_responses, tansu_ssdp_response) responses;
    STAILQ_HEAD(tansu_ssdp_listeners, tansu_ssdp_listener) listeners;
    STAILQ_HEAD(tansu_ssdp_interfaces, tansu_ssdp_interface) interfaces;
    STAILQ_HEAD(tansu_ssdp_ads, tansu_ssdp_ad) ads;
    char *server_name;
    struct tansu_ssdp_arena arena;
    struct tansu_ssdp_addr mcast_addr;
    struct tansu_ssdp_addr mcast_addr6;
};

#ifdef __cplusplus
extern "C" {
#endif

enum tansu_ssdp_search_target tansu_ssdp_get_search_target(const char *st);

struct tansu_ssdp_response *tansu_ssdp_response_new(int st_size);
void tansu_ssdp_response_delete(struct tansu_ssdp_response *res);

struct tansu_ssdp_ad *tansu_ssdp_ad_new(int nt_size);
void tansu_ssdp_ad_delete(struct tansu_ssdp_ad *ad);

struct tansu_ssdp_request *tansu_ssdp_request_new(const char *st);
void tansu_ssdp_request_delete(struct tansu_ssdp_request *req);

struct tansu_bound_socket *
tansu_ssdp_request_get_sock(int fd, struct tansu_ssdp_request *req);

tansu_status_t
tansu_ssdp_request_msearch(struct event_base *evbase,
                           struct tansu_ssdp_request *req,
                           const struct tansu_nwifs *nwifs);

struct tansu_ssdp_interface *
tansu_ssdp_interface_new(int family, const char *name, const char *mcastif);

void
tansu_ssdp_interface_delete(struct tansu_ssdp_interface *iface);


struct tansu_ssdp_server *tansu_ssdp_server_new();
void tansu_ssdp_server_delete(struct tansu_ssdp_server *server);

tansu_status_t
tansu_ssdp_server_start(struct event_base *evbase,
                        struct tansu_ssdp_server *server,
                        struct tansu_nwifs *nwifs);

void
tansu_ssdp_server_stop(struct tansu_ssdp_server *server);

struct tansu_ssdp_listener *
tansu_ssdp_server_add(struct tansu_ssdp_server *server,
                      void (*cb)(const struct tansu_ssdp_packet *, void *),
                      void *cbarg);
void
tansu_ssdp_server_remove(struct tansu_ssdp_server *server,
                         struct tansu_ssdp_listener *listener);
void
tansu_ssdp_server_nwif_changed(struct tansu_ssdp_server *server,
                               struct tansu_nwif_events *events);

void
tansu_ssdp_server_create_interfaces(struct tansu_ssdp_server *server,
                                    struct tansu_nwifs *nwifs);

tansu_status_t
tansu_ssdp_packet_parse(struct tansu_ssdp_packet *packet,
                        enum tansu_ssdp_kind kind,
                        struct tansu_ssdp_arena *arena,
                        char *text);

struct tansu_ssdp_packet *
tansu_ssdp_packet_clone(const struct tansu_ssdp_packet *packet);

void
tansu_ssdp_packet_delete(struct tansu_ssdp_packet *packet);

int
tansu_ssdp_write_request(char *buff,
                         int buffsize,
                         const char *st,
                         const struct tansu_keyvals *headers);

int
tansu_ssdp_write_response(char *buff,
                          int buffsize,
                          int max_age,
                          const char *address,
                          int port,
                          const char *server,
                          const char *st,
                          const char *udn,
                          const struct tansu_keyvals *headers);

int
tansu_ssdp_write_notify(char *buff,
                        int buffsize,
                        int max_age,
                        const char *address,
                        int port,
                        const char *server,
                        const char *nt,
                        const char *nts,
                        const char *udn,
                        const struct tansu_keyvals *headers);

void tansu_ssdp_server_read(int fd, short what, void *arg);

void
tansu_ssdp_server_reply(struct tansu_ssdp_server *server,
                        struct tansu_ssdp_response *res);

void
tansu_ssdp_server_notify(struct tansu_ssdp_server *server,
                         struct tansu_ssdp_ad *ad);

#ifdef __cplusplus
}
#endif

#endif /* _TANSU_SSDP_H_ */

